package com.ruoyi.web.controller.bl;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bl.domain.BlSuppliers;
import com.ruoyi.bl.service.IBlSuppliersService;
import net.bytebuddy.asm.Advice;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bl.domain.BlGoods;
import com.ruoyi.bl.service.IBlGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author ruoyiblGoods
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/bl/goods")
public class BlGoodsController extends BaseController
{
    @Autowired
    private IBlGoodsService blGoodsService;

    @Autowired
    private IBlSuppliersService  blSuppliersService;

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('bl:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlGoods blGoods)
    {
        startPage();
        List<BlGoods> list = blGoodsService.selectBlGoodsList(blGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('bl:goods:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlGoods blGoods)
    {
        List<BlGoods> list = blGoodsService.selectBlGoodsList(blGoods);
        ExcelUtil<BlGoods> util = new ExcelUtil<BlGoods>(BlGoods.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('bl:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(blGoodsService.selectBlGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('bl:goods:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlGoods blGoods)
    {
        long supplierId = blGoods.getSupplierId();
        BlSuppliers blSuppliers = blSuppliersService.selectBlSuppliersBySupplierId(supplierId);
        if(blSuppliers == null){
            return error("所选的供应商不存在");
        }
        if(blSuppliers.getDeptId() == null){
            return error("所选的供应商未绑定商户");
        }
        long deptId = blSuppliers.getDeptId();
        BlGoods tmp = blGoodsService.findBlGoodsByNextGoodsSn();
        blGoods.setGoodsSn(tmp.getGoodsSn());
        blGoods.setDeptId(deptId);
        blGoods.setCreateBy(getUsername());
        return toAjax(blGoodsService.insertBlGoods(blGoods));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('bl:goods:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlGoods blGoods)
    {
        long supplierId = blGoods.getSupplierId();
        BlSuppliers blSuppliers = blSuppliersService.selectBlSuppliersBySupplierId(supplierId);
        if(blSuppliers == null){
            return error("所选的供应商不存在");
        }
        if(blSuppliers.getDeptId() == null){
            return error("所选的供应商未绑定商户");
        }
        long deptId = blSuppliers.getDeptId();
        blGoods.setDeptId(deptId);
        blGoods.setUpdateBy(getUsername());
        return toAjax(blGoodsService.updateBlGoods(blGoods));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('bl:goods:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(blGoodsService.deleteBlGoodsByGoodsIds(goodsIds));
    }

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('bl:goodsMerchant:list')")
    @GetMapping("/merchantList")
    public TableDataInfo merchantList(BlGoods blGoods)
    {
        startPage();
        blGoods.setDeptId(getDeptId());
        List<BlGoods> list = blGoodsService.selectBlGoodsList(blGoods);
        return getDataTable(list);
    }
}
